﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VoteServices.VoteModel;

namespace VoteServices
{
    public class RecordService
    {
        VoteData data = new VoteData();
        public void Voting(User user)
        {
            int a;
           Option answer = new Option();
            if (data.records.Count != 0)
            {
                var B = data.records.Where(p => p.Account == user).Where(p => p.Selection.Id == 0);  //查找此用户没有回答的问题；
                if (B.Count() == 0)
                {
                    Console.WriteLine("每个用户针对每个题目只有一次机会，您已经答题完毕");
                }
                else
                {
                    for (int i = 0; i < B.Count(); i++)
                    {
                        Record b = B.ElementAt(i);
                        Problem question = data.problems.FirstOrDefault(p => p.Id == b.ProblemId);
                        Console.WriteLine(question.Content);
                        foreach (Option choices in data.options.Where(p=>p.ProblemId==question.Id))
                        {
                            Console.WriteLine("{0}:{1}", choices.Id, choices.Content);
                        }
                        Console.WriteLine("*******请输入你的选择*********");
                        a = Convert.ToInt32(Console.ReadLine());
                        answer = data.options.FirstOrDefault(p => p.Id == a);

                        if (answer == null)
                        {
                            Console.WriteLine("你的输入有误，投票失败。");
                        }
                        else
                        {
                            Record conditional = new Record
                            {
                                Account = user,
                                Time = new DateTime(),
                                Selection = answer,
                                ProblemId = question.Id,
                            };
                            data.records.Add(conditional);
                            data.records.Remove(b);
                            answer.Vote++;
                        }
                                  
                    }
                }
            }
            else
            {
                foreach (Problem question in data.problems)
                {
                    Console.WriteLine(question.Content);
                    var B = data.options.Where(p => p.ProblemId== question.Id);
                    foreach (var b in B)
                    {
                        Console.WriteLine("{0}:{1}", b.Id, b.Content);
                    }
                    Console.WriteLine("*******请输入你的选择*********");
                    a = Convert.ToInt32(Console.ReadLine());
                    answer = data.options.FirstOrDefault(p => p.Id == a);
                    if (answer == null)
                    {
                        Console.WriteLine("你的输入有误，投票失败。");
                    }
                    else
                    {
                        Record conditional = new Record
                        {
                            Account = user,
                            Time = new DateTime(),
                            Selection = answer,
                            ProblemId = question.Id,
                        };
                        data.records.Add(conditional);
                        answer.Vote++;
                    }
                }
            }
        }
        public void Statics()
        {
            foreach(Problem question in data.problems)
            {
                foreach(Option choices in data.options.Where(p=>p.ProblemId==question.Id))
                {
                    Console.WriteLine("有{0}人在{1}问题中，选择了{2}！", choices.Vote, question.Content, choices.Content);
                }
            }
        }
    }
}
